package com.yuke.cloud.zuul.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yukemall </br> </p>
 * <p>com.yuke.cloud.zuul.filter Description TODO </br> </p>
 * @Author wg
 * @Date   2019/1/4 16:43
 */
@Slf4j
public class MyCorsFilter extends CorsFilter {

    private static final String WS_PATH = "/webSocketServer";

    public MyCorsFilter(CorsConfigurationSource configSource) {
        super(configSource);
    }

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        String reqUri = request.getRequestURI();

        log.info("MyCorsFilter-doFilterInternal-request.getRequestURI()-reqUri={}", reqUri);
        if (reqUri.contains(WS_PATH)) {
            filterChain.doFilter(request, response);
        }else {
            super.doFilterInternal(request, response, filterChain);
        }
    }
}
